//
//  SYKSShiTiDetailsView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/26.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYKSShiTiDetailsView.h"
#import "SYYueJuanStudentsView.h"
#import "SYKSShiTiTopView.h"//顶部显示正确率错误率
#import "SYYueJuanHtmlTableViewCell.h"


@interface SYKSShiTiDetailsView ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) SYYueJuanStudentsView *studentsView;//左侧学生名字view
@property (strong, nonatomic) SYKSShiTiTopView *cuowuTopView;//正确率view
@property (strong, nonatomic) SYTopView *topView;//标题
@property (strong, nonatomic) UIButton *chakanBtn;//退出
@property (strong, nonatomic) UITableView *curTableView;
@property (strong, nonatomic) SYAnsweredQuestionDetail *questionDetail;//试题详情
@property (strong, nonatomic) SYNoLabel *noLab;

@end


@implementation SYKSShiTiDetailsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}
//fudao_close
- (void)initSubView {
    [self addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(20);
        make.right.left.equalTo(self);
        make.height.mas_equalTo(45);
    }];
    //退出
    [self addSubview:self.chakanBtn];
    [self.chakanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topView.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-25);
        make.width.height.mas_equalTo(40);
    }];
    
    //正确率
    [self addSubview:self.cuowuTopView];
    [self.cuowuTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.left.equalTo(self.mas_left).offset(14);
        make.right.equalTo(self.mas_right).offset(-14);
        make.height.mas_equalTo(40);
    }];
    //姓名
    [self addSubview:self.studentsView];
    [self.studentsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cuowuTopView.mas_bottom);
        make.right.equalTo(self.mas_right).offset(-14);
        make.bottom.equalTo(self.mas_bottom).offset(-14);
        make.width.mas_equalTo(200);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.cuowuTopView.mas_leading);
        make.top.equalTo(self.cuowuTopView.mas_bottom);
        make.right.equalTo(self.studentsView.mas_left);
        make.bottom.equalTo(self.mas_bottom).offset(-14);
    }];
    
    [self.curTableView registerCellWithNIbNames:@[@"SYYueJuanHtmlTableViewCell"]];
    
    self.noLab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
    [self.noLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.left.right.bottom.equalTo(self);
        
    }];
    
    [self hiddenViewsHidden:YES];
}

- (void)hiddenViewsHidden:(BOOL)isHidden {
    self.curTableView.hidden = isHidden;
    self.studentsView.hidden = isHidden;
    self.cuowuTopView.hidden = isHidden;
}

- (void)requestDataTemplateId:(NSString *)templateId studentsIds:(NSString *)studentsId isTongJiType:(BOOL)isType {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requeststatisticsAnswerdQuestionDetailTemplateQuestionId:templateId studentsId:studentsId kaoshiOrDingzheng:isType success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        if (dataListArr.count > 0) {
            weakSelf.questionDetail = dataListArr.firstObject;
            weakSelf.cuowuTopView.detailsModel = weakSelf.questionDetail;
            [weakSelf.studentsView requestDataShiTiDetailsArr:weakSelf.questionDetail.dataStudentsArr select:YES];
            [weakSelf.curTableView reloadData];
            
            [weakSelf hiddenViewsHidden:NO];
            weakSelf.noLab.hidden = YES;
        } else {
            weakSelf.noLab.hidden = NO;
            [weakSelf hiddenViewsHidden:YES];
        }
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [XSInfoView showInfo:error.message onSubView:self];
        weakSelf.noLab.hidden = NO;
        [weakSelf hiddenViewsHidden:YES];
    }];
    
    
}


#pragma mark - 表
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.questionDetail == nil) {
        return 0;
    } else {
        return 3;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *htmle = nil;
    if (indexPath.section == 0) {
        htmle = self.questionDetail.questionContent;
    } else if (indexPath.section == 1) {
        htmle = self.questionDetail.rightAnswerContent;
    } else {
        htmle = self.questionDetail.analysisContent;
    }
    
    
    NSAttributedString *attributed = [self attributedStringWithHTMLString:htmle];
    
    CGFloat height = [attributed  boundingRectWithSize:CGSizeMake(kScreen_WIDTH - 180 , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
    CGFloat ht = 40;
    if (height < 100) {
        return height + 50+ht;
    } else if (height < 200) {
        return height + 100+ht;
    } else {
        return height + 150+ht;
    }
}
//计算富文本高度
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    NSAttributedString *attributed = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    return attributed;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
//    }
//    cell.backgroundColor = [UIColor yellowColor];
    
    
    
    
    if (indexPath.section == 0) {
        SYYueJuanHtmlTableViewCell *htmlCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanHtmlTableViewCell" forIndexPath:indexPath];
        htmlCell.htmlStr = self.questionDetail.questionContent;
        return htmlCell;
    } else if (indexPath.section == 1) {
        SYYueJuanHtmlTableViewCell *htmlCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanHtmlTableViewCell" forIndexPath:indexPath];
        htmlCell.htmlStr = self.questionDetail.rightAnswerContent;
        return htmlCell;
        
    } else {
        SYYueJuanHtmlTableViewCell *htmlCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanHtmlTableViewCell" forIndexPath:indexPath];
         htmlCell.htmlStr = self.questionDetail.analysisContent;
        return htmlCell;
       
    }
    
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    UILabel *headerLab = [UILabel titleColour:TITLECOLOR
                                   titleFount:14
                                     pingfang:@"PingFang-SC-Medium"
                                    alignment:NSTextAlignmentLeft
                                    titleLine:14];
    [headerView addSubview:headerLab];
    [headerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(20);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    if (section == 0) {
        headerLab.text = @"题文";
    } else if (section == 1) {
        headerLab.text = @"答案";
    } else {
        headerLab.text = @"解析";
    }
    headerView.backgroundColor = [UIColor whiteColor];
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    return headerView;
}




- (void)backBtnClick {
    [UIView animateWithDuration:kAnimate animations:^{
        self.frame = CGRectMake(0, kScreen_HEIGHT, APP_WIDTH, APP_HEIGHT-20);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark -

//学生名字
- (SYYueJuanStudentsView *)studentsView {
    if (!_studentsView) {
        _studentsView = [[SYYueJuanStudentsView alloc] init];
    }
    return _studentsView;
}
//标题
- (SYTopView *)topView {
    if (!_topView) {
        _topView = [[SYTopView alloc] init];
        _topView.titleLab.text = @"试题详情";

    }
    return _topView;
}
- (UIButton *)chakanBtn {
    if (!_chakanBtn) {
        _chakanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
//        [_chakanBtn setTitle:@"查看试卷" forState:UIControlStateNormal];
//        [_chakanBtn setTitleColor:[UIColorChange colorwithHexString:@"SELECTEDCOLOR"] forState:UIControlStateNormal];
        [_chakanBtn setImage:[UIImage imageNamed:@"fudao_close"] forState:UIControlStateNormal];
        _chakanBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_chakanBtn setTitleColor:SELECTEDCOLOR forState:0];
        [_chakanBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _chakanBtn;
}

- (SYKSShiTiTopView *)cuowuTopView {
    if (!_cuowuTopView) {
        _cuowuTopView = [[NSBundle mainBundle] loadNibNamed:@"SYKSShiTiTopView" owner:nil options:nil].firstObject;
    }
    return _cuowuTopView;
}

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
//        [_curTableView registerCellWithNIbNames:@[@"SYYiPiGaiTableViewCell"]];
    }
    return _curTableView;
}
@end



















